﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMS.HelperClasses;
using WHMS.EntityClasses;
using WHMS.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace WMS
{
	public partial class MoveItemsForm : Form
	{
		TransferRequestEntity _currentTransferRequest;
		DataMngrFormBase<TransferRequestItemEntity> _tranItemsForm;
		List<ErrorProvider> _errorProviders;

		public MoveItemsForm()
		{
			InitializeComponent();
            HideOrViewWeightCol();
            this._transItems.EntityFactoryToUse = new WHMS.FactoryClasses.TransferRequestItemEntityFactory();

            var label = new Label();
            _tranItemsForm = new DataMngrFormBase<TransferRequestItemEntity>(_transItems, _transItemsGrid, _transferRequestItemsBS, new Button(), label, label, new RelationPredicateBucket(TransferRequestItemFields.TransferRequestId == 0));
			
		}


        private void HideOrViewWeightCol()
        {
            var weightCol = _transItemsGrid.Columns["Weight"];

            if (weightCol != null)
            {
                if (UserInfo.Roles.Where(r => r.ViewWorkshop == true).Count() > 0)
                {
                    weightCol.Visible = true;
                }
                else
                {
                    weightCol.Visible = false;
                }
            }
        }

		private void MoveItemsForm_Load(object sender, EventArgs e)
		{			
			LoadData();
			InitializeDataSources();

			_errorProviders = new List<ErrorProvider>();

			//EnableDisableControls();
		}
        //private void StoresDDL_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    ResetTransactionItems();

        //    if (StoresDDL.SelectedItem == null)
        //        return;
            
        //    FormsHelper.LoadComboBox(ToStoresDDL, StoreFields.Id, StoreFields.Name, () => Business.GetTargetStoresForKeeper(GetSelectedStoreId()));
        //}
		private void ProjectsDDL_SelectedIndexChanged(object sender, EventArgs e)
		{
			ResetTransactionItems();
			EnableDisableControls();
		}

		private void SaveBtn_Click(object sender, EventArgs e)
		{
			if (!ValidateForm())
				return;

			if (!_currentTransferRequest.IsNew)
			{
				if ((bool)DALHelper.GetValue(TransferRequestFields.IsProcessed, TransferRequestFields.Id, _currentTransferRequest.Id))
				{
					RetrieveRequest();
					return;
				}
			}
            _currentTransferRequest.ProjectId = GetSelectedProjectId();
		    _currentTransferRequest.ToProjectId = GetSelectedToProjectId();
            _currentTransferRequest.FromStoreId = GetSelectedStoreId();
            _currentTransferRequest.ToStoreId = GetSelectedToStoreId();
		    _currentTransferRequest.Date = MoveDateDTP.Value;

			_currentTransferRequest.CreatedBy = UserInfo.UserId;

			foreach (TransferRequestItemEntity item in _transItems)
			{
				_currentTransferRequest.TransferRequestItems.Add(item);
			}

			DALHelper.DeleteCollection(_transItems.RemovedEntitiesTracker);

            //var examinationDoc = new ExaminationDocument(_currentTransferRequest);
            //if(examinationDoc.ShowDialog(this)== DialogResult.Cancel)
            //{
            //    MessageBox.Show(@"تم الغاء الحفظ");
            //    examinationDoc.Close();
            //    return;
            //}

		    if (DALHelper.SaveEntity(_currentTransferRequest, true))
			{
				RetrieveRequest();
				MessageBox.Show("تم الحفظ بنجاح.");
			}

			//if (!ValidateForm())
			//    return;

			//var transaction = new TransactionEntity()
			//{
			//    ProjectId = (int)ProjectsDDL.SelectedValue,
			//    PermitNumber = PermitNumberTB.Text,
			//    CreatedBy = UserInfo.UserId,
			//    Date = MoveDateDTP.Value.Date,
			//    Type = "Move"
			//};

			//var transfer = new TransferTransactionEntity()
			//{
			//    //Transaction = transaction,
			//    Type = "Normal",
			//    FromStoreId = (int)StoresDDL.SelectedValue,
			//    ToStoreId = (int)ToStoresDDL.SelectedValue,
			//};

			//if (Business.MoveItems(GetSelectedStoreId(), GetSelectedToStoreId(), transfer, transaction, Business.GetGenericCollection<TransactionItemEntity>(_transItems)))//DALHelper.SaveEntity(withdrawl, true))
			//{
			//    MessageBox.Show("تم الحفظ بنجاح.");

			//    ResetTransactionItems();
			//}
		}
		private void SearchBtn_Click(object sender, EventArgs e)
		{
			var filter = new RelationPredicateBucket(StorekeeperFields.KeeperId == Business.GetCurrentUserId());
			filter.Relations.Add(TransferRequestEntity.Relations.ProjectEntityUsingProjectId);
			filter.Relations.Add(TransferRequestEntity.Relations.StoreEntityUsingFromStoreId);
			filter.Relations.Add(StoreEntity.Relations.StorekeeperEntityUsingStoreId);

			var searchForm = new SearchForm(TransferRequestFields.RequestNumber, TransferRequestFields.ProjectId, TransferRequestFields.Date, filter, () => Business.GetRunningProjects());
			if (searchForm.ShowDialog() == DialogResult.OK)
			{
				PermitNumberTB.Text = searchForm.SelectedNumber;
				RetrieveRequest();

			}
		}
        private void SearchFromProject(object sender, EventArgs e)
        {
            ResetTransactionItems();
            var filter = new RelationPredicateBucket();
            var searchForm = new SearchForm(ProjectFields.Code, ProjectFields.Name, ProjectFields.ActualStartDate, filter,
                                            () => Business.GetRunningAndGeneralProjects());
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtSearchFromProject.Text = searchForm.SelectedNumber;
                
                EnableDisableControls();
            }
        }


        private void SearchToProject(object sender, EventArgs e)
        {
            var filter = new RelationPredicateBucket();
            var searchForm = new SearchForm(ProjectFields.Code, ProjectFields.Name, ProjectFields.ActualStartDate, filter,
                                            null);
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtSearchToProject.Text = searchForm.SelectedNumber;
                EnableDisableControls();
            }
        }

        private void SearchFromStoreClick(object sender, EventArgs e)
        {
            var searchForm = new fromGeneralEntityPicker("Store");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtSearchFromStore.Text = searchForm.SelectedItemCode;

            }
        }

        private void SearchToStoreClick(object sender, EventArgs e)
        {
            var searchForm = new fromGeneralEntityPicker("Store");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtSearchToStore.Text = searchForm.SelectedItemCode;

            }
        }

		private void RetrieveBtn_Click(object sender, EventArgs e)
		{
			RetrieveRequest();
		}
		private void NewBtn_Click(object sender, EventArgs e)
		{
			InitializeDataSources();
			ResetTransactionItems();
		}
		private void DeleteBtn_Click(object sender, EventArgs e)
		{
			var form = new DeleteConfirmationForm();

			if (form.ShowDialog() == DialogResult.Yes)
			{
				if (DALHelper.DeleteEntity(_currentTransferRequest))
				{
					InitializeDataSources();
					ResetTransactionItems();
				}
			}
		}

		private void InitializeDataSources()
		{
			_currentTransferRequest = new TransferRequestEntity(); 			

			BindDataSources();

			EnableDisableControls();
		}
		private void BindDataSources()
		{
            _TransferBS.DataSource = _currentTransferRequest;
		}
		private void EnableDisableControls()
		{
			if (_currentTransferRequest == null)
				return;

			var enabled = (!_currentTransferRequest.IsProcessed && GetSelectedProjectId() > 0 && GetSelectedToProjectId() > 0);
			_transItemsGrid.Enabled = enabled;
			SaveBtn.Enabled = enabled;
			bindingNavigator1.Enabled = enabled;
			_alreadyApprovedLbl.Visible = _currentTransferRequest.IsProcessed;

            btnSearchFromProject.Enabled= txtSearchFromProject.Enabled = txtSearchToProject.Enabled = btnSearchToProject.Enabled = _currentTransferRequest.IsNew;
		    

			NewBtn.Enabled = !_currentTransferRequest.IsNew;
			DeleteBtn.Enabled = !(_currentTransferRequest.IsNew || _currentTransferRequest.IsProcessed);
		}
		void LoadData()
		{
			//FormsHelper.LoadComboBox(StoresDDL, StoreFields.Id, StoreFields.Name, () => Business.GetStoresForKeeper());
			//FormsHelper.LoadComboBox(ProjectsDDL, ProjectFields.Id, ProjectFields.Name, (x, y) => DALHelper.GetDynamicList(x, y, null));
            //FormsHelper.LoadComboBox(toProjectsDdl, ProjectFields.Id, ProjectFields.Name, (x, y) => DALHelper.GetDynamicList(x, y, null));
            //FormsHelper.CheckStoresAndProjects(StoresDDL.Items, ProjectsDDL.Items,
            //                                   new List<Control>() {_transItemsGrid, SaveBtn}, NoStoresLbl, NoProjectsLbl);

            //FormsHelper.CheckStoresAndProjects(StoresDDL.Items, toProjectsDdl.Items,
            //                                   new List<Control>() {_transItemsGrid, SaveBtn}, NoStoresLbl, NoProjectsLbl);

		}

		private void RetrieveRequest()
		{
			if (string.IsNullOrEmpty(PermitNumberTB.Text.Trim()))
				return;

			var collection = new EntityCollection<TransferRequestEntity>();

			var filter = new RelationPredicateBucket(TransferRequestFields.RequestNumber == PermitNumberTB.Text.Trim());

			DALHelper.FetchCollection(collection, filter);

			if (collection.Count <= 0)
			{
				InitializeDataSources();
				return;
			}

			_currentTransferRequest = collection[0];
			BindDataSources();

			_transItems.Clear();

		    var itemsFilter =
		        new RelationPredicateBucket(TransferRequestItemFields.TransferRequestId == _currentTransferRequest.Id);

            itemsFilter.Relations.Add(TransferRequestItemEntity.Relations.ItemEntityUsingItemId);
     
		    DALHelper.FetchCollection(_transItems, itemsFilter);

            if (_transItems != null)
            {
                foreach (var transItem in _transItems)
                {
                    var itemCollection = new EntityCollection<ItemEntity>();

                    int itemId = ((TransferRequestItemEntity) (transItem)).ItemId;

			        var itemFilter = new RelationPredicateBucket(ItemFields.Id == itemId);

                    DALHelper.FetchCollection(itemCollection, itemFilter);

                    if(itemCollection.Count > 0)
                    {
                        ((TransferRequestItemEntity) (transItem)).Item = itemCollection[0];
                    }
                }
            }
            
			ValidateRequestNumber();

			EnableDisableControls();

		    SetProjectsAndStores();
		}

	    private void SetProjectsAndStores()
	    {
	        var fromProj = GetProjectById(_currentTransferRequest.ProjectId);
            txtSearchFromProject.Text = fromProj != null
                                            ? fromProj.Code
	                                        : string.Empty;

	        var toProj = _currentTransferRequest.ToProjectId != null
	                         ? GetProjectById(_currentTransferRequest.ToProjectId.Value)
	                         : null;

            txtSearchToProject.Text = toProj != null
                                            ? toProj.Code
                                            : string.Empty;

            var fromStore = GetStoreById(_currentTransferRequest.FromStoreId);
            txtSearchFromStore.Text = fromStore != null
                                            ? fromStore.Code
                                            : string.Empty;
            var toStore = GetStoreById(_currentTransferRequest.ToStoreId);
            txtSearchToStore.Text = toStore != null
                                            ? toStore.Code
                                            : string.Empty;
	    }

	    private bool ValidateRequestNumber()
		{
			if (!string.IsNullOrEmpty(PermitNumberTB.Text.Trim()))
			{
				if (Business.CheckExisitngTransferRequestNumber(PermitNumberTB.Text.Trim(), _currentTransferRequest.Id))
				{
					var errorProvider = new ErrorProvider(this);
					_errorProviders.Add(errorProvider);

					errorProvider.SetError(PermitNumberTB, "رقم الطلب مستخدم");
					errorProvider.RightToLeft = true;
					return false;
				}
				else
				{
					foreach (var errorProvider in _errorProviders)
					{
						if (!string.IsNullOrEmpty(errorProvider.GetError(PermitNumberTB)))
						{
							errorProvider.SetError(PermitNumberTB, string.Empty);
						}
					}
				}
			}
			return true;
		}
		private bool ValidateForm()
		{
			var isValid = true;

			var controls = new List<TextBox>();
			controls.Add(PermitNumberTB);

			if (_errorProviders == null)
			{
				_errorProviders = new List<ErrorProvider>();
			}
			else
			{
				foreach (var errorProvider in _errorProviders)
				{
					errorProvider.Clear();
					errorProvider.Dispose();
				}
			}
			foreach (var tb in controls)
			{
				if (string.IsNullOrEmpty(tb.Text.Trim()))
				{
					var errorProvider = new ErrorProvider(this);
					_errorProviders.Add(errorProvider);

					errorProvider.SetError(tb, "يجب إدخال قيمة");
					errorProvider.RightToLeft = true;

					isValid = false;
					continue;
				}
			}


			if (!ValidateRequestNumber())
				isValid = false;


			_tranItemsForm.ValidateCollection();

			if (_transItems.Count <= 0)
			{
				isValid = false;
				MessageBox.Show("يجب إضافة أصناف", "خطأ فى الحفظ", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

            if (string.IsNullOrEmpty(txtSearchToStore.Text))
            {
                isValid = false;
                MessageBox.Show("يجب اختيار قيمة للمخزن ", "خطأ فى الحفظ", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

			return isValid;
		}
		
		private void ResetTransactionItems()
		{
			if (_tranItemsForm != null)
			{
				_tranItemsForm.Dispose();
				_tranItemsForm = null;
			}
		    txtSearchFromProject.Text = string.Empty;
		    txtSearchFromStore.Text = string.Empty;
		    txtSearchToProject.Text = string.Empty;
		    txtSearchToStore.Text = string.Empty;

			var label = new Label();
			_tranItemsForm = new DataMngrFormBase<TransferRequestItemEntity>(_transItems, _transItemsGrid, _transferRequestItemsBS, new Button(), label, label, new RelationPredicateBucket(TransferRequestItemFields.TransferRequestId == 0));
			_tranItemsForm.SetNumericFields(new List<int> {4});
			_tranItemsForm.SetMandatoryFields(new List<int>() { 0, 4 });			
			_tranItemsForm.RowValidating += new DataMngrFormBase<TransferRequestItemEntity>.RaiseRowValidating(_tranItemsForm_RowValidating);
		}

		void _tranItemsForm_RowValidating(int rowIndex, ref bool hasErrors)
		{
            var requestedQuantityCell = _transItemsGrid.Rows[rowIndex].Cells["quantityDataGridViewTextBoxColumn"];
            var availableQuantity = _transItemsGrid.Rows[rowIndex].Cells["AvailableAmount"].Value;
			var requestedQuantity = requestedQuantityCell.Value;

			if (availableQuantity == null || requestedQuantity == null)
				return;

			if ((decimal)requestedQuantity <= 0)
			{
				requestedQuantityCell.ErrorText = "الكمية المطلوبة يجب أن تكون رقم موجب.";
				hasErrors = true;
				return;
			}

			if ((decimal)availableQuantity >= (decimal)requestedQuantity)
			{
				requestedQuantityCell.ErrorText = string.Empty;
				return;
			}

			requestedQuantityCell.ErrorText = "الكمية المطلوبة يجب أن لا تتعدى الكمية المتاحة.";
			hasErrors = true;
		}

		private int GetSelectedStoreId()
		{
            var store = GetStoreByCode(txtSearchFromStore.Text.Trim());
            if (store != null)
                return store.Id;
            return 0;

            //if (StoresDDL.SelectedItem == null)
            //    return 0;

            //return (int)((System.Data.DataRowView)(StoresDDL.SelectedItem)).Row.ItemArray[0];
		}
		private int GetSelectedToStoreId()
		{
            var store = GetStoreByCode(txtSearchToStore.Text.Trim());
            if (store != null)
                return store.Id;
            return 0;

            //if (ToStoresDDL.SelectedItem == null)
            //    return 0;

            //return (int)((System.Data.DataRowView)(ToStoresDDL.SelectedItem)).Row.ItemArray[0];
		}
        private int GetSelectedProjectId()
        {
            var proj = GetProjectByCode(txtSearchFromProject.Text.Trim());
            if (proj != null)
                return proj.Id;
            return 0;
            //if (ProjectsDDL.SelectedItem == null)
            //    return 0;

            //return (int)((System.Data.DataRowView)(ProjectsDDL.SelectedItem)).Row.ItemArray[0];
        }

	    private ProjectEntity GetProjectById(int id)
        {
            var collection = new EntityCollection<ProjectEntity>();

            var filter = new RelationPredicateBucket(ProjectFields.Id == id);

            DALHelper.FetchCollection(collection, filter);

            if (collection.Count != 0)
                return collection.First();

            return null;
        }

        private ProjectEntity GetProjectByCode(string code)
        {
            var collection = new EntityCollection<ProjectEntity>();

            var filter = new RelationPredicateBucket(ProjectFields.Code == code);

            DALHelper.FetchCollection(collection, filter);

            if (collection.Count != 0)
                return collection.First();

            return null;
        }

        private StoreEntity GetStoreByCode(string code)
        {
            var collection = new EntityCollection<StoreEntity>();

            var filter = new RelationPredicateBucket(StoreFields.Code == code);

            DALHelper.FetchCollection(collection, filter);

            if (collection.Count != 0)
                return collection.First();

            return null;
        }

        private StoreEntity GetStoreById(int id)
        {
            var collection = new EntityCollection<StoreEntity>();

            var filter = new RelationPredicateBucket(StoreFields.Id == id);

            DALHelper.FetchCollection(collection, filter);

            if (collection.Count != 0)
                return collection.First();

            return null;
        }

        private int GetSelectedToProjectId()
        {
            var proj = GetProjectByCode(txtSearchToProject.Text.Trim());
            if (proj != null)
                return proj.Id;
            return 0;
            //if (toProjectsDdl.SelectedItem == null)
            //    return 0;

            //return (int)((System.Data.DataRowView)(toProjectsDdl.SelectedItem)).Row.ItemArray[0];
        }

		private void PermitNumberTB_Leave(object sender, EventArgs e)
		{
			ValidateRequestNumber();
		}

		private void _transItemsGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == quantityDataGridViewTextBoxColumn.Index)
			{
                
				var itemIdValue = _transItemsGrid.Rows[e.RowIndex].Cells[ItemIdColumn.Index].Value;
				if (itemIdValue == null)
					return;

				var form = new UnitConverterForm((int)itemIdValue);
				if (form.ShowDialog() == DialogResult.OK)
				{
					_transItemsGrid.SuspendLayout();
					_transItemsGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = form.Value;
					_transItemsGrid.UpdateCellValue(e.ColumnIndex, e.RowIndex);
					_transItemsGrid.ResumeLayout(true);
				}
			}
			else if (e.ColumnIndex == ItemCode.Index)//ItemName.Index)
			{
				var form = new ItemPickerForm(1);
				if (form.ShowDialog() == DialogResult.OK)
				{
					GetItemDetails(e.RowIndex, form.SelectedItemId);
					//var _requestItem = (WithdrawRequestItemEntity)_requestItems[e.RowIndex];
					//_requestItem.ItemNameCodeUnit = form.SelectedItemName;
					//_requestItem.ItemId = form.SelectedItemId;

					_transItemsGrid.BeginEdit(false);

					SendKeys.Send("{LEFT}");
					SendKeys.Send("{LEFT}");
					SendKeys.Send("{LEFT}");

					var qty = _transItemsGrid.Rows[e.RowIndex].Cells[quantityDataGridViewTextBoxColumn.Index].Value.ToString();
					var chars = qty.ToCharArray();
					foreach (char c in chars)
					{ 
						SendKeys.Send(c.ToString());
					}
					SendKeys.Send("{ENTER}");
				}
			}

		}

		private void _transItemsGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == ItemCode.Index)
			{
                if(_transItemsGrid.Rows[e.RowIndex].Cells[ItemCode.Index].Value!=null)
                {
                    var code = _transItemsGrid.Rows[e.RowIndex].Cells[ItemCode.Index].Value.ToString();
                    GetItemDetails(e.RowIndex, code);
                    return;
                }
			}
            else if(e.ColumnIndex==quantityDataGridViewTextBoxColumn.Index)
            {
                var itemCollection = new EntityCollection<ItemEntity>();

                var itemId = ((TransferRequestItemEntity) (_transItems[e.RowIndex])).ItemId;

                var itemFilter = new RelationPredicateBucket(ItemFields.Id == itemId);

                DALHelper.FetchCollection(itemCollection, itemFilter);

                if(itemCollection.Count > 0)
                {
                    ((TransferRequestItemEntity)(_transItems[e.RowIndex])).Item = itemCollection[0];
                }
            }
		}

		private void _transItemsGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
		{
			foreach (DataGridViewRow row in _transItemsGrid.Rows)
			{
				var item = (TransferRequestItemEntity)row.DataBoundItem;
				if (item == null)
					continue;

				GetItemDetails(row.Index, item.ItemId);
			}
		}

		private void GetItemDetails(int rowIndex, int itemId)
		{
			var dt = Business.GetItemDetails(itemId);
			if (dt.Rows.Count <= 0)
				return;

			_transItemsGrid.Rows[rowIndex].Cells[ItemCode.Index].Value = (string)dt.Rows[0][0];
			_transItemsGrid.Rows[rowIndex].Cells[ItemName.Index].Value = (string)dt.Rows[0][1];
			_transItemsGrid.Rows[rowIndex].Cells[ItemUnit.Index].Value = (string)dt.Rows[0][2];
			return;
		}

		private void GetItemDetails(int rowIndex, string itemCode)
		{
			var dt = Business.GetItemDetails(itemCode, 1);
			if (dt.Rows.Count <= 0)
				return;

			_transItemsGrid.Rows[rowIndex].Cells[ItemIdColumn.Index].Value = (int)dt.Rows[0][0];
			_transItemsGrid.Rows[rowIndex].Cells[ItemName.Index].Value = (string)dt.Rows[0][1];
			_transItemsGrid.Rows[rowIndex].Cells[ItemUnit.Index].Value = (string)dt.Rows[0][2];
			return;
		}

        private void txtSearchFromProject_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtSearchFromProject.Text))
            {
                ProjectEntity project = Business.GetProjectByCode(txtSearchFromProject.Text);

                if (project!=null)
                {
                    lblFromProjectName.Text = project.Name;
                    EnableDisableControls();
                }
                else
                {
                    lblFromProjectName.Text = string.Empty;
                }
            }
        }

        private void txtSearchToProject_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtSearchToProject.Text))
            {
                ProjectEntity project = Business.GetProjectByCode(txtSearchToProject.Text);

                if (project != null)
                {
                    lblToProjectName.Text = project.Name;
                    EnableDisableControls();
                }
                else
                {
                    lblToProjectName.Text = string.Empty;
                }
            }

        }

        private void txtSearchFromStore_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtSearchFromStore.Text))
            {
                DataTable DTStore = Business.GetStoreDetails(txtSearchFromStore.Text);
                if (DTStore.Rows.Count > 0)
                {
                    //selectedStoreID = Int32.Parse(DTStore.Rows[0][0].ToString());
                    lblFromStoreName.Text = DTStore.Rows[0][2].ToString();
                    //RunBtn.Enabled = true;
                    EnableDisableControls();
                }
                else
                {
                    lblFromStoreName.Text = string.Empty;
                    //RunBtn.Enabled = false;
                }
            }
        }

        private void txtSearchToStore_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtSearchToStore.Text))
            {
                DataTable DTStore = Business.GetStoreDetails(txtSearchToStore.Text);
                if (DTStore.Rows.Count > 0)
                {
                    //selectedStoreID = Int32.Parse(DTStore.Rows[0][0].ToString());
                    lblToStoreName.Text = DTStore.Rows[0][2].ToString();
                    //RunBtn.Enabled = true;
                    EnableDisableControls();
                }
                else
                {
                    lblToStoreName.Text = string.Empty;
                    //RunBtn.Enabled = false;
                }
            }
        }

        

	}
}
